/**
 * @file     containsNearByDuplicate.ts
 * @brief    [219. 存在重复元素 II](https://leetcode.cn/problems/contains-duplicate-ii/description/)
 * @author   Zhu
 * @date     2022-11-05 23:40
 */

function containsNearbyDuplicate(nums: number[], k: number): boolean {
    const set = new Set();

    for (let i = 0; i < nums.length; i++) {
        if (i > k) set.delete(nums[i - k - 1]);
        if (set.has(nums[i])) return true;
        set.add(nums[i]);
    }

    return false;
}
